$(function() {
	// 1.查询国家
	$.get("/JavaWeb项目_第六组/hotel", {
		method: "findCountry"
	}, function(data) {
		if(data == '' || data == null) {
			return;
		}
		var lis = '';
		// 遍历数组,拼接字符串
		for(var i = 0; i < data.length; i++) {
			var li = '<li><a href="javascript:showCity(' + data[i].country_id + ');" data-bn-ipg="zsj-list-freedomtravel-cd1" class="SxIzBNt35cIvti5M0JRUX">' + data[i].country_name + '</a></li>';
			lis += li;
		}
		// 显示国家
		$("#country").html(lis);
	});
	// 2、显示所有的分页商品
	addAllPages();
	// 3、近期好货
	getGoodThings();
});

/**
 * 显示城市
 * @param {Object} country_id
 */
function showCity(country_id) {
	if(country_id != 0) {
		// 显示所有的城市
		$.get("/JavaWeb项目_第六组/hotel", {
			country_id,
			method: "findCity"
		}, function(data) {
			// 显示商品
			$("#showStore").css("display", "block");
			var lis = '';
			for(var i = 0; i < data.length; i++) {
				// 方法一 travel_Store_jd.jsp?city_id='+data[i].city_id+'
				// 方法二 
				var li = '<li><a href="javascript:addPages(' + data[i].city_id + ');" data-bn-ipg="zsj-list-freedomtravel-cd2" class="SxIzBNt35cIvti5M0JRUX">' + data[i].city_name + '</a></li>';
				lis += li;
			}
			// 显示该国家下的所有商品
			findCountryStore(country_id);
			$("#allCity").css("display", "block"); //显示城市的div
			// 直接显示城市数据
			$("#city").html(lis);
		});
	} else {
		$("#allCity").css("display", "none"); //隐藏城市的div
		// 并且显示所有商品
		addAllPages();
	}
}

/**
 * 查询该国家下的所有商品
 */

function findCountryStore(country_id, currentPage) {
	// 循环的时候点击页码   传递了currentPage
	$.get("/JavaWeb项目_第六组/hotel", {
		method: "findCountryStore",
		currentPage: currentPage,
		country_id: country_id
	}, function(pb) {
		var page = '';
		var beforeNum = pb.currentPage - 1; // 上一页的页数=当前页-1
		if(beforeNum <= 0) {
			beforeNum = 1;
		}
		// 首页
		var firstPage;
		// 上一页
		var prePage;
		// 判断当前页是否为首页
		if(pb.currentPage == 1) {
			firstPage = '<a href="javascript:;" class="ui_page_item ui_page_next" >首页</a>';
			prePage = '<a href="javascript:;" class="ui_page_item ui_page_next">上一页</a>';
		} else {
			firstPage = '<a href="#" onclick="findCountryStore(' + country_id + ',1)" class="ui_page_item">首页</a>';
			prePage = '<a href="#" onclick="findCountryStore(' + country_id + ',' + beforeNum + ')" class="ui_page_item">上一页</a>';
		}
		page += firstPage;
		page += prePage;

		// 1:前5后4的形式
		var begin; // 开始页码
		var end; // 结束页码
		// 页面不足 10===> 从1开始到pb.totalPage结束
		if(pb.totalPage < 10) {
			begin = 1;
			end = pb.totalPage;
		} else {
			// 1.1页面足够
			begin = pb.currentPage - 5;
			end = pb.currentPage + 4;
			// 1.2前面没有5个
			if(begin <= 0) {
				begin = 1;
				end = 10;
			}
			// 1.3后面没有4个
			if(end > pb.totalPage) {
				end = pb.totalPage;
				begin = pb.totalPage - 9;
			}
		}

		for(var i = begin; i <= end; i++) {
			// 异步提交
			var li;
			if(pb.currentPage == i) {
				// 有选中的样式
				var li = '<a href="#" onclick="findCountryStore(' + country_id + ',' + i + ')" class="ui_page_item ui_page_item_current">' + i + '</a>';
			} else {
				// 没有样式
				var li = '<a href="#" onclick="findCountryStore(' + country_id + ',' + i + ')" class="ui_page_item">' + i + '</a>';
			}
			page += li;
		}

		var nextNum = pb.currentPage + 1; // 当前页+1
		if(nextNum > pb.totalPage) {
			nextNum = pb.totalPage;
		}
		// 下一页
		var nextPage;
		// 上一页
		var lastPage;
		// 判断当前页是否为尾页
		if(pb.currentPage == pb.totalPage) {
			nextPage = '<a href="javascript:;" class="ui_page_item ui_page_next">下一页</a>';
			lastPage = '<a href="javascript:;" class="ui_page_item ui_page_next">尾页</a>';
		} else {
			//方法1 herf="#" onclick="addPages('+city_id+','+pb.totalPage+')"
			//方法2 window.scrollTo(0,0);
			nextPage = '<a href="javascript:findCountryStore(' + country_id + ',' + nextNum + ');" class="ui_page_item">下一页</a>';
			lastPage = '<a href="javascript:findCountryStore(' + country_id + ',' + pb.totalPage + ');" class="ui_page_item">尾页</a>';
		}
		page += nextPage;
		page += lastPage;
		page += '<a href="" class="ui_page_item"></a>';
		var totalPage = '<a href="javascript:;" class="ui_page_item" >共 ' + pb.totalPage + ' 页 ' + pb.totalCount + ' 条</a>';
		page += totalPage;
		// 加载到对应位置
		$("#pages").html(page);
		// 显示完分页再加载页面商品信息
		// 1、拿到数据
		var divs = '';
		for(var i = 0; i < pb.list.length; i++) {
			var div = '';
			div += '<div class="zw-new-bigcard-item">';
			div += '<a href="travel_Store_jpyd.jsp?hotel_id=' + pb.list[i].hotel_id+'" class="new-bigcard-left">';
			div += '<img width="225" height="150" title="" alt="" class="zw-new-bigcard-itemimg" src="/JavaWeb项目_第六组/hotel?method=showImage&ticketShowPic=' + pb.list[i].hotel_img + '">';
			div += '<div class="bottom-text">';
			div += '<div class="left">酒店</div>';
			div += '<div class="right" id="data' + pb.list[i].hotel_id + '"></div>';
			div += '</div>';
			div += '</a>';
			div += '<div class="zw-new-bigcard-iteminfo clearfix">';
			div += '<div class="zw-new-bigcard-tag-wrap">';
			div += '<div class="zw-new-bigcard-h2ul-wrap">';
			div += '<div class="title">';
			div += '<a href="travel_Store_jpyd.jsp?hotel_id=' + pb.list[i].hotel_id +'" title="' + pb.list[i].hotel_text + '" target="_blank" data-bn-ipg="zsj-list-freedomtravel-pos1" data-ra_arg="">' + pb.list[i].hotel_text + '</a>';
			div += '</div>';
			div += '</div>';
			div += '<div class="zw-new-bigcard-infonum"><span>' + pb.list[i].hotel_sale_number + '</span>件已售';
			div += '</div>';
			div += '</div>';
			div += '<div class="zw-new-bigcard-bottombar">';
			div += '<div class="zw-new-bigcard-price"><span style="color:#ff7362;font-size: 20px;">￥</span><span><em>' + pb.list[i].hotel_price + '</em>元起</span>';
			div += '</div>';
			div += '<a href="javascript:aClick(' + pb.list[i].hotel_id + ');" class="zw-new-bigcard-btn">立即预订</a>';
			div += '</div>';
			div += '</div>';
			div += '</div>';
			divs += div;
			findHotelLabel(pb.list[i]);
		}
		// 2、拼接数据
		$("#showStore").html(divs);
		// 方法2 定位到页面顶部
		window.scrollTo(0, 0);
	});
}

/**
 * 显示所有的商品,未选中城市
 */
function showAllStore(list) {
	// 先隐藏商品
	$("#showStore").css("display", "none");
	// 1、拿到数据
	var divs = '';
	for(var i = 0; i < list.length; i++) {
		var div = '';
		div += '<div class="zw-new-bigcard-item">';
		div += '<a href="travel_Store_jpyd.jsp?hotel_id=' + list[i].hotel_id +'" class="new-bigcard-left">';
		div += '<img width="225" height="150" title="" alt="" class="zw-new-bigcard-itemimg" src="/JavaWeb项目_第六组/hotel?method=showImage&ticketShowPic=' + list[i].hotel_img + '">';
		div += '<div class="bottom-text">';
		div += '<div class="left">酒店</div>';
		div += '<div class="right" id="data' + list[i].hotel_id + '"></div>';
		div += '</div>';
		div += '</a>';
		div += '<div class="zw-new-bigcard-iteminfo clearfix">';
		div += '<div class="zw-new-bigcard-tag-wrap">';
		div += '<div class="zw-new-bigcard-h2ul-wrap">';
		div += '<div class="title">';
		div += '<a href="travel_Store_jpyd.jsp?hotel_id=' + list[i].hotel_id +'" title="' + list[i].hotel_text + '" target="_blank" data-bn-ipg="zsj-list-freedomtravel-pos1" data-ra_arg="">' + list[i].hotel_text + '</a>';
		div += '</div>';
		div += '</div>';
		div += '<div class="zw-new-bigcard-infonum"><span>' + list[i].hotel_sale_number + '</span>件已售';
		div += '</div>';
		div += '</div>';
		div += '<div class="zw-new-bigcard-bottombar">';
		div += '<div class="zw-new-bigcard-price"><span style="color:#ff7362;font-size: 20px;">￥</span><span><em>' + list[i].hotel_price + '</em>元起</span>';
		div += '</div>';
		div += '<a href="javascript:aClick(' + list[i].hotel_id + ');" class="zw-new-bigcard-btn">立即预订</a>';
		div += '</div>';
		div += '</div>';
		div += '</div>';
		divs += div;
		findHotelLabel(list[i]);
	}
	// 2、显示商品
	$("#showStore").html(divs);
	$("#showStore").css("display", "block");
}

/**
 * 分页拼接 无参数
 */
function addAllPages(currentPage) {
	$.get("/JavaWeb项目_第六组/hotel", {
		method: "allPageQuery",
		currentPage: currentPage
	}, function(pb) {
		var page = '';
		var beforeNum = pb.currentPage - 1; // 上一页的页数=当前页-1
		if(beforeNum <= 0) {
			beforeNum = 1;
		}
		// 首页
		var firstPage;
		// 上一页
		var prePage;
		// 判断当前页是否为首页
		if(pb.currentPage == 1) {
			firstPage = '<a href="javascript:;" class="ui_page_item ui_page_next" >首页</a>';
			prePage = '<a href="javascript:;" class="ui_page_item ui_page_next">上一页</a>';
		} else {
			firstPage = '<a href="#" onclick="addAllPages(1)" class="ui_page_item">首页</a>';
			prePage = '<a href="#" onclick="addAllPages(' + beforeNum + ')" class="ui_page_item">上一页</a>';
		}

		page += firstPage;
		page += prePage;

		// 前5后4的页码数据展示

		var begin; // 开始位置
		var end; // 结束位置

		// 1、要显示10个   
		if(pb.totalPage < 10) {
			// 没有10页
			begin = 1;
			end = pb.totalPage;
		} else {
			// 超过10页 前五后4
			begin = pb.currentPage - 5;
			end = pb.currentPage + 4;
			// 1.前面不足5个
			if(begin < 1) {
				begin = 1;
				end = 10;
			}
			// 2.后面不足4个
			if(end > pb.totalPage) {
				end = pb.totalPage;
				begin = end - 9;
			}
		}

		for(var i = begin; i <= end; i++) {
			var li;
			// 判断当前页码是否等于i
			if(pb.currentPage == i) {
				// 有样式
				li = '<a href="#" onclick="addAllPages(' + i + ')" class="ui_page_item ui_page_item_current">' + i + '</a>';
			} else {
				// 没有样式
				li = '<a href="#" onclick="addAllPages(' + i + ')" class="ui_page_item">' + i + '</a>';
			}
			// 字符串拼接
			page += li;
		}

		// 显示所有页码
		//		for(var i = 1; i <= pb.totalPage; i++) {
		//			//方法1 javascript:addAllPages('+i+');
		//			//方法2 添加onclick点击事件--->并返回顶部
		//			var li;
		//			// 判断当前页码是否等于i
		//			if (pb.currentPage == i) {
		//				// 有样式
		//				li = '<a href="#" onclick="addAllPages('+i+')" class="ui_page_item ui_page_item_current">'+i+'</a>';
		//			} else{
		//				// 没有样式
		//				li = '<a href="#" onclick="addAllPages('+i+')" class="ui_page_item">'+i+'</a>';
		//			}
		//				page += li;
		//		}

		var nextNum = pb.currentPage + 1; // 当前页+1
		if(nextNum > pb.totalPage) {
			nextNum = pb.totalPage;
		}
		// 下一页
		var nextPage;
		// 上一页
		var lastPage;
		// 判断当前页是否为尾页
		if(pb.currentPage == pb.totalPage) {
			nextPage = '<a href="javascript:;" class="ui_page_item ui_page_next">下一页</a>';
			lastPage = '<a href="javascript:;" class="ui_page_item ui_page_next">尾页</a>';
		} else {
			nextPage = '<a href="#" onclick="addAllPages(' + nextNum + ')" class="ui_page_item">下一页</a>';
			lastPage = '<a href="#" onclick="addAllPages(' + pb.totalPage + ')" class="ui_page_item">尾页</a>';
		}
		page += nextPage;
		page += lastPage;
		page += '<a href="" class="ui_page_item"></a>';
		var totalPage = '<a href="javascript:;" class="ui_page_item" >共 ' + pb.totalPage + ' 页 ' + pb.totalCount + ' 条</a>';
		page += totalPage;
		// 加载到对应位置
		$("#pages").html(page);
		// 显示完分页后显示商品
		showAllStore(pb.list);
	});
}

/**
 * 显示所有的商品,未选中城市
 */
function findStoreByCityId(list) {
	// 1、拿到数据
	var divs = '';
	for(var i = 0; i < list.length; i++) {
		var div = '';
		div += '<div class="zw-new-bigcard-item">';
		div += '<a href="travel_Store_jpyd.jsp?hotel_id=' + list[i].hotel_id + '" class="new-bigcard-left">';
		div += '<img width="225" height="150" class="zw-new-bigcard-itemimg" src="/JavaWeb项目_第六组/hotel?method=showImage&ticketShowPic=' + list[i].hotel_img + '">';
		div += '<div class="bottom-text">';
		div += '<div class="left">酒店</div>';
		div += '<div class="right" id="data' + list[i].hotel_id + '"></div>';
		div += '</div>';
		div += '</a>';
		div += '<div class="zw-new-bigcard-iteminfo clearfix">';
		div += '<div class="zw-new-bigcard-tag-wrap">';
		div += '<div class="zw-new-bigcard-h2ul-wrap">';
		div += '<div class="title">';
		div += '<a href="travel_Store_jpyd.jsp?hotel_id=' + list[i].hotel_id +'" title="' + list[i].hotel_text + '" target="_blank" data-bn-ipg="zsj-list-freedomtravel-pos1" data-ra_arg="">' + list[i].hotel_text + '</a>';
		div += '</div>';
		div += '</div>';
		div += '<div class="zw-new-bigcard-infonum"><span>' + list[i].hotel_sale_number + '</span>件已售';
		div += '</div>';
		div += '</div>';
		div += '<div class="zw-new-bigcard-bottombar">';
		div += '<div class="zw-new-bigcard-price"><span style="color:#ff7362;font-size: 20px;">￥</span><span><em>' + list[i].hotel_price + '</em>元起</span>';
		div += '</div>';
		div += '<a href="javascript:aClick(' + list[i].hotel_id + ');" class="zw-new-bigcard-btn">立即预订</a>';
		div += '</div>';
		div += '</div>';
		div += '</div>';
		divs += div;
		findHotelLabel(list[i]);
	}
	// 2、拼接数据
	$("#showStore").html(divs);
}

/**
 * 分页拼接  有参数
 */
function addPages(city_id, currentPage) {
	$.get("/JavaWeb项目_第六组/hotel", {
		method: "pageQuery",
		city_id,
		currentPage: currentPage
	}, function(pb) {
		var page = '';
		var beforeNum = pb.currentPage - 1; // 上一页的页数=当前页-1
		if(beforeNum <= 0) {
			beforeNum = 1;
		}
		// 首页
		var firstPage;
		// 上一页
		var prePage;
		// 判断当前页是否为首页
		if(pb.currentPage == 1) {
			firstPage = '<a href="javascript:;" class="ui_page_item ui_page_next" >首页</a>';
			prePage = '<a href="javascript:;" class="ui_page_item ui_page_next">上一页</a>';
		} else {
			firstPage = '<a href="#" onclick="addPages(' + city_id + ',1)" class="ui_page_item">首页</a>';
			prePage = '<a href="#" onclick="addPages(' + city_id + ',' + beforeNum + ')" class="ui_page_item">上一页</a>';
		}
		page += firstPage;
		page += prePage;

		// 1:前5后4的形式
		var begin; // 开始页码
		var end; // 结束页码
		// 页面不足 10===> 从1开始到pb.totalPage结束
		if(pb.totalPage < 10) {
			begin = 1;
			end = pb.totalPage;
		} else {
			// 1.1页面足够
			begin = pb.currentPage - 5;
			end = pb.currentPage + 4;
			// 1.2前面没有5个
			if(begin <= 0) {
				begin = 1;
				end = 10;
			}
			// 1.3后面没有4个
			if(end > pb.totalPage) {
				end = pb.totalPage;
				begin = pb.totalPage - 9;
			}
		}

		for(var i = begin; i <= end; i++) {
			// 异步提交
			var li;
			if(pb.currentPage == i) {
				// 有选中的样式
				var li = '<a href="#" onclick="addPages(' + city_id + ',' + i + ')" class="ui_page_item ui_page_item_current">' + i + '</a>';
			} else {
				// 没有样式
				var li = '<a href="#" onclick="addPages(' + city_id + ',' + i + ')" class="ui_page_item">' + i + '</a>';
			}
			page += li;
		}

		// 显示所所有页码
		//		for(var i = 1; i <= pb.totalPage; i++) {
		//			// 异步提交
		//			var li;
		//			if (pb.currentPage == i) {
		//				// 有选中的样式
		//				var li = '<a href="#" onclick="addPages('+city_id+','+i+')" class="ui_page_item ui_page_item_current">'+i+'</a>';
		//			} else{
		//				// 没有样式
		//				var li = '<a href="#" onclick="addPages('+city_id+','+i+')" class="ui_page_item">'+i+'</a>';
		//			}
		//			page += li;
		//		}

		var nextNum = pb.currentPage + 1; // 当前页+1
		if(nextNum > pb.totalPage) {
			nextNum = pb.totalPage;
		}
		// 下一页
		var nextPage;
		// 上一页
		var lastPage;
		// 判断当前页是否为尾页
		if(pb.currentPage == pb.totalPage) {
			nextPage = '<a href="javascript:;" class="ui_page_item ui_page_next">下一页</a>';
			lastPage = '<a href="javascript:;" class="ui_page_item ui_page_next">尾页</a>';
		} else {
			//方法1 herf="#" onclick="addPages('+city_id+','+pb.totalPage+')"
			//方法2 window.scrollTo(0,0);
			nextPage = '<a href="javascript:addPages(' + city_id + ',' + nextNum + ');" class="ui_page_item">下一页</a>';
			lastPage = '<a href="javascript:addPages(' + city_id + ',' + pb.totalPage + ');" class="ui_page_item">尾页</a>';
		}
		page += nextPage;
		page += lastPage;
		page += '<a href="" class="ui_page_item"></a>';
		var totalPage = '<a href="javascript:;" class="ui_page_item" >共 ' + pb.totalPage + ' 页 ' + pb.totalCount + ' 条</a>';
		page += totalPage;
		// 加载到对应位置
		$("#pages").html(page);
		// 显示完分页再加载页面商品信息
		findStoreByCityId(pb.list);
		// 方法2 定位到页面顶部
		window.scrollTo(0, 0);
	});
	// 显示商品信息
}
/**
 * 得到商品的标签
 */
function findHotelLabel(list) {
	var hotel_id = list.hotel_id;
	// 显示a标签的hotel_id
	$.get("/JavaWeb项目_第六组/hotel", {
		method: "findHotelLabel",
		hotel_id
	}, function(label) {
		var ids = "#data" + (list.hotel_id);
		for(var i = 0; i < label.length; i++) {
			$(ids).html(label[i].txt);
		}
	});
}

/**
 * 近期好货添加
 * @param {Object} list
 */
function getGoodThings() {
	var c_id = 1;
	$.get("/JavaWeb项目_第六组/hotel",{method:"getFreshProduct",city_id:c_id},function (list) {
		var lis = '';
		for(var i = 6; i < 11; i++) {
			var li = '';
			li += '<li class="recommendation-item clearfix">';
			li += '<a href="travel_Store_jpyd.jsp?hotel_id=' + list[i].hotel_id +'"><img class="item-img" src="/JavaWeb项目_第六组/hotel?method=showImage&ticketShowPic=' + list[i].hotel_img + '">';
			li += '<div class="item-con">';
			li += '<h6 title="ll.title" class="item-title">'+list[i].hotel_text+'</h6>';
			li += '<div class="item-price">¥<span><em>'+list[i].hotel_price+'</em>元起</span></div>';
			li += '</div>';
			li += '</a>';
			li += '</li>';
			lis+=li;
		}
		$("#goodThings").html(lis);
	});
	
	

}